package com.sunqizhong.controller;

import com.sunqizhong.entity.Emp;
import com.sunqizhong.entity.EmpQueryParam;
import com.sunqizhong.entity.PageBean;
import com.sunqizhong.entity.Result;
import com.sunqizhong.service.EmpLogService;
import com.sunqizhong.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
public class EmpContoller {
    @Autowired
    private EmpService empService;

    /*
    * 分页查询
    * */
/*    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("分页查询:{},{}",page,pageSize);
        PageBean pageBean = empService.page(page,pageSize);
        return Result.success(pageBean);
    }*/

/*    @GetMapping("/emps")
    public Result page(String name,Integer gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                        @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize
                        ){
        log.info("分页查询:{},{},{},{},{},{}",name,gender,begin,end,page,pageSize);
        //PageBean pageBean = empService.page(name,gender,begin,end,page,pageSize);
        return Result.success();
    }*/

    @GetMapping("/emps")
    public Result page(EmpQueryParam param){
        log.info("分页查询:{},{},{},{},{},{}",param.getName(),param.getGender(),param.getBegin(),param.getEnd(),param.getPage(),param.getPageSize());
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }

    /*
    * 添加员工
    * */
    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp){
        log.info("新增员工:{}",emp);
        empService.save(emp);
        return Result.success();
    }

    @DeleteMapping("/emps")
    //public Result delete(@RequestParam List<Integer> ids){ //通过集合接收前端传递的数组值，需要加注解@RequestParam
    public Result delete(Integer[] ids){  //通过数据接收前端传递的数组值
        log.info("ids={}", Arrays.toString(ids));
        empService.delete(ids);
        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("回显员工id = {}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        log.info("修改员工：emp={}",emp);
        empService.update(emp);
        return Result.success();
    }
}
